<?php
/**
 *
 * 版权所有：顺势网站管理系统<www.51shunshi.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台公用控制器。
 *
 **/

namespace Home\Controller;

use Think\Controller;

class ComController extends Controller
{

    public function _initialize()
    {

        if(stripos($_SERVER['PATH_INFO'],"/")){
            $router = explode("/",$_SERVER['PATH_INFO']);
        }else{
            $router = explode(".",$_SERVER['PATH_INFO']);
        }
        $active = $router[0];
        if(in_array($router[0],array('news_list','news','zixun','qianyue'))){
            $active = "news";
        }
        if(in_array($router[0],array('service','service_sem','service_shop','service_sem','service_system','service_weixin'))){
            $active = "service";
        }
        if(in_array($router[0],array('case_shop','case_dingzhi','case_weixin'))){
            $active = "cases";
        }
        $category_news = M("category")->select();
        $category_case = M("category_case")->select();
        $this->assign('category_news',$category_news);
        $this->assign('category_case',$category_case);
        $this->assign('router',$router[0]);
        $this->assign('active',$active);
    }

    protected function searchBy( $keywords, $p = 1 ){

        if(!empty($keywords)){
            $p = intval($p) > 0 ? $p : 1;
            $pagesize = 10;#每页数量
            $offset = $pagesize * ($p - 1);//计算记录偏移量
            $articleDB = M('article as a');
            $orderby = "a.aid DESC";
            $map['a.title'] = array('like',"%".$keywords."%");
            $map['a.description'] = array('like',"%".$keywords."%");
            $map['_logic'] = 'OR';
            $prefix = C('DB_PREFIX');
            $field = "a.aid,a.title,a.description,a.thumbnail,a.t,a.point,a.browse,{$prefix}category.name,{$prefix}category.dir";
            $count = $articleDB->where($map)->count();
            $list = $articleDB
                ->field($field)
                ->where($map)
                ->order($orderby)
                ->join("{$prefix}category ON {$prefix}category.id = a.sid")
                ->limit($offset . ',' . $pagesize)
                ->select();
            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $list[$key]['title'] = str_replace($keywords, "<em>" . $keywords . "</em>", $value['title']);
                    $list[$key]['description'] = str_replace($keywords, "<em>" . $keywords . "</em>", $value['description']);
                    $list[$key]['time'] = mdate(strtotime($value['t']));
                }
            }else{
                return ["code"=>400, "msg"=>"搜索结果为空"];
            }
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
            return ["list"=>$list,"page"=>$page];
        }else{
            return ["code"=>400, "msg"=>"关键词为空"];
        }
    }

    protected function searchLbP( $id, $p = 1 ){
        //点击标签查出所有带此标签的文章
        if(!empty($id)){
            $p = intval($p) > 0 ? $p : 1;
            $pagesize = 10;#每页数量
            $offset = $pagesize * ($p - 1);//计算记录偏移量
            $aPDB = M("adviser_product as ap");
            $prefix = C('DB_PREFIX');
            $field = "{$prefix}article.aid,{$prefix}article.title,{$prefix}article.description,{$prefix}article.thumbnail,{$prefix}article.t,{$prefix}article.point,{$prefix}article.browse,{$prefix}category.dir,{$prefix}category.name";
            $list = $aPDB
                ->where(array("pro_id"=>$id))
                ->field($field)
                ->order("{$prefix}article.aid DESC")
                ->join("{$prefix}article ON {$prefix}article.aid = ap.adviser_id")
                ->join("{$prefix}category ON {$prefix}category.id = {$prefix}article.sid")
                ->limit($offset . ',' . $pagesize)
                ->select();

            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $list[$key]['time'] = mdate(strtotime($value['t']));
                }
            }else{
                return ["code"=>400, "msg"=>"搜索结果为空"];
            }
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
            return ["list"=>$list,"page"=>$page];
        }else{
            return ["code"=>400, "msg"=>"关键词为空"];
        }
    }
}